package com.aim.service.system;

import com.aim.common.base.BaseCrudService;
import com.aim.common.enums.BizExceptionEnum;
import com.aim.common.exception.ServiceException;
import com.aim.config.shiro.ShiroKit;
import com.aim.dao.system.SysUserDao;
import com.aim.entity.system.SysUser;
import org.springframework.stereotype.Service;

/**
 * @AUTO 用户表服务实现类
 * @Author AIM
 * @DATE 2019/04/30
 */
@Service
public class SysUserService extends BaseCrudService<SysUserDao, SysUser> {

    /**
     * 修改密码
     */
    public void changePwd(String oldPassword, String newPassword) {
        Long userId = ShiroKit.getUserNotNull().getUserId();
        SysUser user = this.get(userId);

        String oldMd5 = ShiroKit.md5(oldPassword, user.getSalt());

        if (user.getPassword().equals(oldMd5)) {
            String newMd5 = ShiroKit.md5(newPassword, user.getSalt());
            user.setPassword(newMd5);
            this.update(user);
        } else {
            throw new ServiceException(BizExceptionEnum.OLD_PWD_NOT_RIGHT);
        }
    }
}